<template>
  <el-header class="me-area">
    <el-row class="me-header">
      <el-col v-if="!simple" :span="16">
        <el-menu
          :router="true"
          menu-trigger="click"
          active-text-color="#5FB878"
          :default-active="activeIndex"
          mode="horizontal"
          class="top-menu"
        >
          <el-menu-item index="/" @click="changeHeaderHidden(true)"
            >首页</el-menu-item
          >
          <el-menu-item index="/category/all" @click="changeHeaderHidden(false)"
            >文章分类</el-menu-item
          >
          <el-menu-item index="/tag/all" @click="changeHeaderHidden(false)"
            >标签</el-menu-item
          >
          <el-menu-item index="/archives" @click="changeHeaderHidden(false)"
            >文章归档</el-menu-item
          >

          <el-col
            :span="4"
            :offset="4"
            @click="() => (this.headerHidden = false)"
          >
            <el-menu-item index="/write"
              ><i class="el-icon-edit"></i>写文章</el-menu-item
            >
          </el-col>
        </el-menu>
      </el-col>

      <template v-else>
        <slot></slot>
      </template>

      <el-col :span="4" :offset="4">
        <el-menu
          :router="true"
          menu-trigger="click"
          mode="horizontal"
          active-text-color="#5FB878"
        >
          <template v-if="!user.login" class="user-operate">
            <el-menu-item index="/login">
              <el-button type="text">登录</el-button>
            </el-menu-item>
            <el-menu-item index="/register">
              <el-button type="text">注册</el-button>
            </el-menu-item>
          </template>

          <template v-else>
            <el-submenu index>
              <template slot="title">
                <img class="me-header-picture" :src="user.avatar" />
              </template>
              <el-menu-item index @click="logout"
                ><i class="el-icon-back"></i>退出</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-col>
    </el-row>
  </el-header>
</template>

<script>
export default {
  name: "BaseHeader",
  props: {
    activeIndex: String,
    simple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      headerHidden: this.$router.history.current.path === "/"
    };
  },
  computed: {
    user() {
      let login = this.$store.state.account.length != 0;
      let avatar = this.$store.state.avatar;
      return {
        login,
        avatar
      };
    }
  },
  mounted() {
    this.judgeChangeHeader();
  },
  updated() {
    this.judgeChangeHeader();
  },
  methods: {
    logout() {
      let that = this;
      this.$store
        .dispatch("logout")
        .then(() => {
          this.$router.push({ path: "/" });
        })
        .catch(error => {
          if (error !== "error") {
            that.$message({ message: error, type: "error", showClose: true });
          }
        });
    },
    // 滚动事件
    onScroll() {
      const a = document.documentElement.scrollTop || document.body.scrollTop;
      document.querySelector(".me-area").style.marginLeft =
        a > 400 ? "" : "-100%";
    },
    // 判断是否需要开启动态头部
    judgeChangeHeader() {
      if (this.headerHidden) {
        document.querySelector(".me-area").style.marginLeft = "-100%";
        window.addEventListener("scroll", this.onScroll);
      } else {
        document.querySelector(".me-area").style.marginLeft = "0";
        window.removeEventListener("scroll", this.onScroll);
      }
    },
    // 修改HeaderHidde值
    changeHeaderHidden(flag) {
      this.headerHidden = flag;
    }
  }
};
</script>

<style scoped>
.me-area {
  position: relative;
  z-index: 9999;
}
.el-header {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  transition: all 0.5s;
  min-width: 100%;
  /* background-color: transparent; */
  box-shadow: 0 2px 3px hsla(0, 0%, 7%, 0.1), 0 0 0 1px hsla(0, 0%, 7%, 0.1);
}
.me-title {
  margin-top: 10px;
  font-size: 24px;
}

.me-header-left {
  margin-top: 10px;
}

.me-title img {
  max-height: 2.4rem;
  max-width: 100%;
}

.me-header-picture {
  width: 36px;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 50%;
  vertical-align: middle;
  background-color: #5fb878;
}

.el-menu--horizontal {
  padding-left: 80px;
}
</style>
